﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Xml;

namespace WindowsFormsApplication1
{
    public class CTerrainElement
    {
        //  1  ,        1  ,   1   ,   1    ,   1   ,   1   ,   1   ,    1
        // 普通阻挡, 左变速, 右变速,  惯性  ,  伤害 , 传送  ,  弹簧 ,  死亡, 不可穿越

        // 死亡地面比较特殊，就是说碰到后直接死亡，并且角色消失。
        public const UInt16 NormalTerrain = 1;
        public const UInt16 LeftSpeedTerrain = 2;
        public const UInt16 RightSpeedTerrain = 4;
        public const UInt16 HurtTerrain = 8;
        public const UInt16 SpringTerrain = 16;
        public const UInt16 InertnessTerrain = 32;
        public const UInt16 DeathTerrain = 64;


        public Bitmap bmpTerrainElement;
        public Bitmap bmpProperty;


        public String mFileName
        {
            get;
            set;
        }

        public String mBmpName
        {
            get;
            set;
        }

        public class Block
        {
            public UInt16 BT = 0;       // 阻挡类型
            public int Value = 0;       // 附加值
        }

        public Block[,] mBlocks
        {
            get;
            set;
        }




        public void Save(String xmlName)
        {
            using ( XmlTextWriter xmlwrite = new XmlTextWriter(xmlName, Encoding.Default) )
            {
                xmlwrite.Formatting = Formatting.Indented;
                xmlwrite.WriteStartDocument();
                xmlwrite.WriteStartElement("TerrainElement");
                xmlwrite.WriteStartElement("Pic");
                xmlwrite.WriteString(mBmpName);
                xmlwrite.WriteEndElement();

                xmlwrite.WriteStartElement("Blocks");

                xmlwrite.WriteAttributeString("Row", mBlocks.GetLength(1).ToString());
                xmlwrite.WriteAttributeString("Col", mBlocks.GetLength(0).ToString());

                for (int i = 0; i < mBlocks.GetLength(1); i++)
                {
                    xmlwrite.WriteStartElement("Row");

                    String val = "";

                    for (int j = 0; j < mBlocks.GetLength(0); j++)
                    {
                        if (mBlocks[j, i] != null)
                        {
                            val += mBlocks[j, i].BT + "," + mBlocks[j, i].Value + ",";
                        }
                        else
                        {
                            val += "0,0,";
                        }
                    }

                    xmlwrite.WriteString(val);
                    xmlwrite.WriteEndElement();
                }
                xmlwrite.WriteEndElement();
                xmlwrite.WriteEndElement();
                xmlwrite.Close();
            }
        }

        public bool ReadElementBitmap(String bmpName)
        {
            Bitmap bmpTemp = null;
            try
            {
                bmpTemp = new Bitmap(CONST.ROOTPATH + CONST.TERRAINPATH + bmpName);
            }
            catch(System.Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message);
                return false;
            }
            

            if (bmpTemp.Width % 16 != 0 || bmpTemp.Height % 16 != 0)
            {
                System.Windows.Forms.MessageBox.Show(CONST.BMPDIVIDEBY16,
                    "地表元素图片：" + bmpName + "出错！");
                return false;
            }

            if (bmpTemp.Width > 1000 || bmpTemp.Height > 600)
            {
                System.Windows.Forms.MessageBox.Show(CONST.BMPTOOBIG,
                    "地表元素图片：" + bmpName + "出错！");
                return false;
            }

            bmpTerrainElement = bmpTemp;
            bmpProperty = new Bitmap(bmpTerrainElement.Width, bmpTerrainElement.Height);

            Graphics g = Graphics.FromImage(bmpProperty);
            g.Clear(Color.FromArgb(0, 0xFF, 0xFF, 0xFF));
            Pen pen = new Pen(Color.Black, 1);

            for (int i = 16; i <= bmpProperty.Width; i += 16)
            {
                g.DrawLine(pen, i, 0, i, bmpProperty.Height);
            }
            for (int i = 16; i <= bmpProperty.Height; i += 16)
            {
                g.DrawLine(pen, 0, i, bmpProperty.Width, i);
            }

            return true;
        }

        public bool Read(String xmlName)
        {
            using (XmlTextReader xmlread = new XmlTextReader(CONST.ROOTPATH + CONST.TERRAINPATH + xmlName))
            {
                xmlread.ReadStartElement("TerrainElement");
                xmlread.ReadStartElement("Pic");
                mBmpName = xmlread.ReadString();
                if ( !ReadElementBitmap(mBmpName) )
                {
                    return false;
                }
                xmlread.ReadEndElement();

                int Row;
                int Col;

                if (xmlread.IsStartElement("Blocks"))
                {
                    Row = Convert.ToUInt16(xmlread.GetAttribute(0));
                    Col = Convert.ToInt32(xmlread.GetAttribute(1));
                }
                else
                    return false;

                mFileName = xmlName;
                mBlocks = new Block[Row, Col];

                xmlread.Read();

                for (int i = 0; i < Row; i++)
                {
                    xmlread.ReadStartElement("Row");

                    String row = xmlread.ReadString();

                    String[] elem = row.Split(',');
                    for (int j = 0; j < Col; j++)
                    {
                        if (elem[j * 2] == "0")
                        {
                            continue;
                        }
                        else
                        {
                            mBlocks[i, j] = new Block();
                            mBlocks[i, j].BT = Convert.ToUInt16(elem[j * 2]);
                            mBlocks[i, j].Value = Convert.ToInt32(elem[j * 2 + 1]);
                        }
                    }

                    xmlread.ReadEndElement();
                }
                xmlread.ReadEndElement();
                xmlread.ReadEndElement();
                xmlread.Close();
            }
            return true;
        }

    }
}
